package day0228;

public class Exer03 {

    public static void main(String[] args) {
        String str = """
                Java语言是面向对象的，Java语言是健壮的，Java语言是安全的，Java是高性能的，Java语言是跨平台的
                """;
        System.out.println(getCount2(str, "Java"));
    }

    public static int getCount(String str, String subString) {
        if (str != null && subString != null){
            int oldLength = str.length();
            int newLength = str.replace(subString, "").length();
            return (oldLength - newLength) / subString.length();
        }
        return -1;
    }

    public static int getCount2(String str, String subString) {
        if (str != null && subString != null){
            int count = 0;
            int length = subString.length();

            int index = -1;
            while((index = str.indexOf(subString)) != -1){
                str = str.substring(index + length);
                count ++;
            }
            return count;
        }
        return -1;
    }
}
